package com.woniuxy.mesboot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.mesboot.dto.FirstInspDTO;
import com.woniuxy.mesboot.entity.MesFirstArticleInspection;
import com.woniuxy.mesboot.service.FirstArticleInspectionService;
import com.woniuxy.mesboot.util.Result;
import com.woniuxy.mesboot.vo.FirstInspVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("first")
public class FirstArticleInspectionController {
    @Resource
    private FirstArticleInspectionService firstArticleInspectionService;

    @PostMapping("queryAll")
    public Result queryAll(@RequestBody FirstInspDTO inspDTO) {
        PageHelper.startPage(inspDTO.getCurrentPage(),inspDTO.getPageSize(),true);
        List<FirstInspVO> i = firstArticleInspectionService.selectAll(inspDTO);
        PageInfo<FirstInspVO> pageInfo = new PageInfo<>(i);
        return Result.ok().data("list", i).data("total",pageInfo.getTotal());

    }

    @PostMapping
    public Result insert(@RequestBody MesFirstArticleInspection record) {
        int insert = firstArticleInspectionService.insertSelective(record);
        if (insert > 0) {
            return Result.ok("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @DeleteMapping("{inspectionFirstId}")
    public Result delete(@PathVariable("inspectionFirstId") Long id) {
        int i = firstArticleInspectionService.deleteByPrimaryKey(id);
        if (i > 0) {
            return Result.ok("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    @GetMapping("{inspectionFirstId}")
    public Result queryById(@PathVariable("inspectionFirstId") Long id) {
        final MesFirstArticleInspection mesFirstArticleInspection = firstArticleInspectionService.selectByPrimaryKey(id);
        return Result.ok().data("item", mesFirstArticleInspection);
    }

    @PutMapping
    public Result update(@RequestBody MesFirstArticleInspection record) {
        int i = firstArticleInspectionService.updateByPrimaryKey(record);
        if (i > 0) {
            return Result.ok("修改成功");

        } else {
            return Result.error("修改失败");
        }
    }
}
